import numpy as np

def train_test_split(X, y, test_ratio = 0.2, seed = None):
    """将数据X和y按照test_ratio分给成X_train, X_test, y_train, y_test"""
    assert X.shape[0] == y.shape[0], "数据集中样本行数和结果数应该相等"
    assert 0.0 <= test_ratio <= 1.0, "测试数据集占比应该在0%-100%之间"

    if seed:
        np.random.seed(seed)

    shuffled_indexes = np.random.permutation(len(X))

    test_size = int(len(X) * test_ratio)
    test_indexes = shuffled_indexes[:test_size]
    train_indexexs = shuffled_indexes[test_size:]

    X_train = X[train_indexexs]
    y_train = y[train_indexexs]

    X_test = X[test_indexes]
    y_test = y[test_indexes]

    return X_train, X_test, y_train, y_test